package cn.itcast.zt.java8.model.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 动态代理拦截器实现
 * create by zhangtian105 on 2021/9/15
 */
public class SalaryInterceptor implements InvocationHandler {

    private Object target ;
    private List<Interceptor> interceptors ;

    public SalaryInterceptor(Object target, List<Interceptor> interceptors) {
        this.target = target ;
        this.interceptors = interceptors ;
    }

    /**
     * 该方法的内容会成为代理对象的方法体的内容
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        /**
         * 拦截器栈
         */
        for (Interceptor interceptor : interceptors) {
            interceptor.interceptor();
        }

        method.invoke(this.target, args) ;
        return null;
    }
}
